﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Globalization;

namespace NPMonitor
{
    public static class StringExtensions
    {
        public static int? ToInteger(this string str)
        {
            if (str == null)
            {
                return null;
            }

            int rtnValue = 0;
            if (int.TryParse(str, out rtnValue))
            {
                return rtnValue;
            }

            return null;
        }

        public static string ToReportDateFormat(this string str)
        {
            return str.ToDateTimeByFormat().Value.ToString("ddd, dd-MM-yyyy",NPMonitor.Helpers.Utils.SystemCulture);
        }

        public static string ToReportDatetimeFormat(this string str)
        {
            return str.ToDateTimeByFormat().Value.ToString("ddd, dd-MM-yyyy HH:mm", NPMonitor.Helpers.Utils.SystemCulture);
        }

        public static DateTime? ToDateTimeByFormat(this string str)
        {
            if (str == null)
            {
                return null;
            }

            string tStr = str.Trim();
            string format = "dd-MM-yyyy HH:mm:ss".Substring(0, tStr.Length);

            DateTime rtnValue = DateTime.Now;

            if (DateTime.TryParseExact(tStr, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out rtnValue))
            {
                return rtnValue;
            }

            return null;
        }

        public static DateTime? ToDateTimeByFormat(this string str, string format)
        {
            if (str == null)
            {
                return null;
            }

            DateTime rtnValue = DateTime.Now;

            if (DateTime.TryParseExact(str, format, DateTimeFormatInfo.InvariantInfo, DateTimeStyles.None, out rtnValue))
            {
                return rtnValue;
            }

            return null;
        }

        public static DateTime? ToDateTime(this string str)
        {
            if (str == null)
            {
                return null;
            }

            DateTime rtnValue = DateTime.Now;

            if (DateTime.TryParse(str, out rtnValue))
            {
                return rtnValue;
            }

            return null;
        }

        public static string Limit(this string str, int length, string postfix = "...")
        {
            string rtnStr = str ?? "";

            if (rtnStr.Length > length)
            {
                rtnStr = rtnStr.Substring(0, length);
                rtnStr += postfix;
            }

            return rtnStr;
        }

        public static string ToNullFilteredString(this object obj)
        {
            string rtnStr = "";

            if (obj != null)
            {
                rtnStr = obj.ToString();
            }

            return rtnStr;
        }

        public static string FormatString(this string str, params object[] args)
        {
            string rtnStr = str ?? "";

            rtnStr = String.Format(rtnStr, args);

            return rtnStr;
        }

        public static string ToFormatString(this Nullable<DateTime> dt)
        {
            if (dt.HasValue)
            {
                return dt.Value.ToString("dd-MM-yyyy HH:mm:ss");
            }
            return string.Empty;
        }

        public static string ToFormatString(this Nullable<DateTime> dt,string format)
        {
            if (dt.HasValue)
            {
                return dt.Value.ToString(format);
            }
            return DateTime.UtcNow.ToString(format);
        }

        public static string ToFormatString(this DateTime dt)
        { 
            return dt.ToString("dd-MM-yyyy HH:mm:ss");
        }

        public static string ToDateString(this DateTime dt)
        {
            return dt.ToString("dd-MM-yyyy");
        }

        public static string ToYearlyDateString(this DateTime dt)
        {
            return dt.ToString("MM-yyyy");
        }

        public static string ToDateHourString(this DateTime dt)
        {
            return dt.ToString("dd-MM-yyyy HH:mm");
        }

        public static string Join(this IEnumerable objEnum)
        {
            return objEnum.Join(',');
        }

        public static string Join(this IEnumerable objEnum, char divChar)
        {
            string rtn = string.Empty;

            foreach (object tobj in objEnum)
            {
                if (tobj != null && !string.IsNullOrEmpty(tobj.ToString().Trim()))
                {
                    rtn += string.Format("{0}{1}", tobj.ToString(), divChar);
                }
            }

            rtn = rtn.TrimEnd(divChar);

            return rtn;
        }

        public static string Join(this IEnumerable objEnum, string divStr)
        {
            string rtn = string.Empty;

            foreach (object tobj in objEnum)
            {
                if (tobj != null && !string.IsNullOrEmpty(tobj.ToString().Trim()))
                {
                    rtn += string.Format("{0}{1}",tobj.ToString(),divStr);
                }
            }

            return rtn.Remove(rtn.LastIndexOf(divStr));
        }

        public static DateTime GetFirstDayOfThisWeek(this DateTime dt)
        {
            int weekNow = Convert.ToInt32(dt.DayOfWeek);
            int dayDiff = (-1) * weekNow + 1;
            if (weekNow != 1)
            {
                return dt.AddDays(dayDiff);
            }
            else
                return dt;
            
        }

        public static DateTime GetFirstDayOfThisMonth(this DateTime dt)
        {
            int monthNow = dt.Month;
            int yearNow = dt.Year;
            string strFirstDayOfMonth = string.Format("{0}-{1}-01",yearNow,monthNow);
            DateTime firstDayOfMonth = DateTime.ParseExact(strFirstDayOfMonth, "yyyy-M-dd", null);
            return firstDayOfMonth;
        }

        public static DateTime GetFirstMonthOfThisYear(this DateTime dt)
        {
            if (dt.Month != 1)
            {
                return dt.AddMonths((-1) * dt.Month + 1).AddDays((-1) * dt.Day + 1);
            }
            else
                return dt.AddDays(dt.Day * (-1) + 1);
        }

        public static DateTime GetBeginTimeOfHourlyReport(this DateTime dt)
        {
            //if (dt.Hour != 1)
            //{
            //    return dt.AddHours(-1);
            //}
            //else
            //    return dt.AddMinutes(dt.Minute * (-1));
            return dt.AddMinutes((-1) * dt.Minute);
        }

        public static DateTime GetBeginTimeOfDailyReport(this DateTime dt)
        {
            return dt.Date.AddHours(0).AddMinutes(0);
        }
    }
}
